import vue from '@vitejs/plugin-vue2'
import ViteCommonjs from 'vite-plugin-commonjs';
import viewHtmlPlugin from './scripts/vite-plugins/view.js';
import mockPlugin from 'vite-plugin-mock-zhiniu';
import { defineConfig, loadEnv } from "vite"

import mockRouter from "./mock/routers.js"
import path from 'path';

/** @type {import('vite').UserConfig} */
export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd(), '')
    const { API_BASE_URL } = env
    const PORT = env.PORT || 5002

    return {
        define: {
            process: {
                env: {
                    MY_BUILD_MODE: "viteLocal"
                }
            }
        },
        test: {

        },
        root: './',
        server: {
            host: '0.0.0.0',
            fs: {
                allow: ['..']
            },
            port: PORT,
            proxy: {
                "/api": {
                    target: API_BASE_URL,
                    changeOrigin: true,
                    secure: false, // 添加此行以忽略证书错误
                },
                "/libs": {
                    target: `http://localhost:${PORT}`,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/libs/, 'deliver-center-view/src/libs')
                },
            },
            watch: {
                // 直接指定需要监听的路径
                paths: [path.resolve(__dirname, './deliver-center-view/src/')],
            }
        },
        plugins: [
            ViteCommonjs(),
            vue(),
            viewHtmlPlugin,
            mockPlugin({
                routerMap: mockRouter
            }),
        ],
        resolve: {
            extensions: ['.vue', '.js'],
            alias: {
                // 全部使用绝对路径
                '@deliverCenter': path.resolve(__dirname, './deliver-center-view/src/views/deliver-center/'),
                "@commonComponents": path.resolve(__dirname, './deliver-center-view/src/views/_components/'),
                "@common": path.resolve(__dirname, './deliver-center-view/src/views/_common/'),
                "@bingostoreBase": path.resolve(__dirname, './deliver-center-view/src/views/deliver-center/base/'),
                '@utils': path.resolve(__dirname, './deliver-center-view/src/base/utils'),
                '@jarvisBase': path.resolve(__dirname, './deliver-center-view/src/views/jarvis/base'),
                '@/base': path.resolve(__dirname, './deliver-center-view/src/base'),
                '@base': path.resolve(__dirname, './deliver-center-view/src/base'),
                '~@workbench': path.resolve(__dirname, './deliver-center-view/src/base/static/images/workbench'),
                '@workbench': path.resolve(__dirname, './deliver-center-view/src/base/static/images/workbench'),
            },
        },
    }
})